/****** 接口请求和响应文件 ******/
import api from '@/common/js/api.js'
import obj from "@/common/js/obj.js"
import fun from "@/common/js/fun.js"

let baseURL = "https://xq.xuequekeji.xyz/xqcc/xqhome/public/index.php/applet/"   // 测试域名
// 正式环境自动切换正式域名
let { miniProgram } = uni.getAccountInfoSync();
if(miniProgram.envVersion=='release') {
	baseURL = "https://xq.xuequekeji.xyz/xqhome/public/index.php/applet/"  	// 正式域名
}

export default {
	/****** post请求 start ******/
	post: function(link, parameter={}, header) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseURL+api[link],
				data: parameter,
				method: "POST",
				success: res => {
					if(res.statusCode==200) {
						resolve(res.data)
					}else{
						uni.showToast({ title: '系统发生错误',duration: 2000, icon:'none' });
					}
				},
				fail: err => {
					uni.showToast({ title: '网络繁忙',duration: 2000, icon:'none' });
					reject(err)
				}
			})
		})
	},
	/****** post请求 end ******/
	
	/****** 上传文件 start ******/
	file: function(file) {
		uni.showLoading({ title: '正在上传...', mask: true });
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: baseURL+'file_upload/uploadFileOss',
				filePath: file,
				name: 'file',
				success: function (res) {
					uni.hideLoading();
					if(res.statusCode==200) {
						resolve(JSON.parse(res.data));
					}else{
						uni.showToast({ title: '解析错误,请重新上传',duration: 1200, icon:'none' });
						reject(res)
					}
				},
				fail: function (err) {
					uni.hideLoading();
					uni.showToast({ title: '上传失败',duration: 2000, icon:'none' });
					reject(err)
				},
			});
		})
	},
	/****** 上传文件 end ******/
}